package com.tools.db.ip;

import com.tools.common.object.Note;
import com.tools.common.security.SysKit;

import java.util.Objects;

/**
 * IP 地址信息实体对象
 * */
@Note("IP 地址信息实体对象")
public class IPBean {

    @Note("主键 ID，用 IP 转数字来充当")
    private Integer id;

    @Note("IP 地址字符串表现形式")
    private String ip;

    @Note("IP 地址所在国家名称")
    private String country;

    @Note("IP 地址所在国家的省份名称")
    private String province;

    @Note("IP 地址所在国家的城市名称")
    private String city;

    @Note("IP 地址所在国家-城市-区县名称")
    private String region;

    @Note("IP 地址的运营商名称")
    private String isp;


    public IPBean() {}

    public IPBean(String stringResult) {
        this(stringResult, "\\|");
    }

    public IPBean(String stringResult, String separator) {
        if(stringResult == null || stringResult.isEmpty()) throw new IllegalArgumentException("IP 信息字符串为空");
        if(separator == null || separator.isEmpty()) throw new IllegalArgumentException("分隔符为空");
        IPKit.default_fillInIPData(this, stringResult, separator);
    }

    public static IPBean of() {
        return new IPBean();
    }

    public static IPBean of(String stringResult) {
        return new IPBean(stringResult);
    }

    public static IPBean of(String stringResult, String separator) {
        return new IPBean(stringResult, separator);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setId(int id) { this.id = id; }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setIp(int ip) {
        this.ip = SysKit.toStringIp(ip);
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getIsp() {
        return isp;
    }

    public void setIsp(String isp) {
        this.isp = isp;
    }

    @Override
    public String toString() {
        return "IPBean{" +
                "id=" + id +
                ", ip=" + ip +
                ", country=" + country +
                ", province=" + province +
                ", city=" + city +
                ", region=" + region +
                ", isp=" + isp +
                '}';
    }

    public String asString() {
        return this.asString("|");
    }

    public String asString(String separator) {
        if(separator == null || separator.isEmpty()) throw new IllegalArgumentException("分隔符为空");
        StringBuilder builder = new StringBuilder();
        private_addData(builder, separator, country);
        private_addData(builder, separator, province);
        private_addData(builder, separator, city);
        private_addData(builder, separator, region);
        private_addData(builder, separator, isp);
        return builder.toString();
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        IPBean ipBean = (IPBean) o;
        return Objects.equals(id, ipBean.id) && Objects.equals(ip, ipBean.ip) && Objects.equals(country, ipBean.country) && Objects.equals(province, ipBean.province) && Objects.equals(city, ipBean.city) && Objects.equals(region, ipBean.region) && Objects.equals(isp, ipBean.isp);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, ip, country, province, city, region, isp);
    }


    public IPBean id(int id) {
        this.setId(id);
        return this;
    }

    public IPBean id(Integer id) {
        this.setId(id);
        return this;
    }

    public IPBean ip(String ip) {
        this.setIp(ip);
        return this;
    }

    public IPBean ip(int ip) {
        this.setIp(ip);
        return this;
    }

    public IPBean country(String country) {
        this.setCountry(country);
        return this;
    }

    public IPBean province(String province) {
        this.setProvince(province);
        return this;
    }

    public IPBean city(String city) {
        this.setCity(city);
        return this;
    }

    public IPBean region(String region) {
        this.setRegion(region);
        return this;
    }

    public IPBean isp(String isp) {
        this.setIsp(isp);
        return this;
    }


    @Note("根据 data 是否为 null 拼接不同的值")
    private void private_addData(StringBuilder builder, String separator, String data) {
        if(data != null && !data.isEmpty()) {
            builder.append(data).append(separator);
        } else {
            builder.append('0').append(separator);
        }
    }
}
